package com.kbt.net;

/**
 * Request ?????????
 */

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;

import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by zhang_q
 */
public class RequestManager {

    /**
     * ??? *
     */
    public ResponseInterface mResponseInterface;

    /**
     * Gson???? *
     */
    private Gson gson;

    /**
     * ?????
     */
    public RequestManager() {
        gson = new Gson();

    }
    // -------------------------??????????????��???????---------------------------------------------

    /**
     * @param requestType ???????GET or POST??
     * @param url         ?????URL
     * @param tag         TAG
     * @param map         ???
     * @param cls         ????
     * @param <T>         ??????
     */
    public <T> void requestGet(int requestType, String url,
                               final int tag, Map<String, Object> map, Class<T> cls) {
        GsonRequest<T> request = new GsonRequest<T>(requestType,
               url, cls, new Response.Listener<T>() {

					public void onResponse(T t) {
						// TODO Auto-generated method stub
			                mResponseInterface.successResponse(t, tag);
					}
                
        }, new Response.ErrorListener() {

            public void onErrorResponse(VolleyError arg0) {
                mResponseInterface
                        .errorResonse(Constants.NetError, tag);
            }
        });
        request.setTag(tag);
        request.setRetryPolicy(new DefaultRetryPolicy(100000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        BaseApplication.getRequestQueuemanager().add(request);
    }



    public <T> void post(String url,
                         final Map<String, String> map,
                         final int tag,
                         Class<T> cls) {
        PostRequest<T> request = new PostRequest<T>(
                Request.Method.POST,
                url,
                map,
                cls,
                new Response.Listener<T>() {
                    public void onResponse(T t) {
                        mResponseInterface.successResponse(t, tag);
                    }
                },
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError arg0) {
                        // TODO Auto-generated method stub
                        mResponseInterface.errorResonse(arg0.getMessage(), tag);
                    }
                }
        );
        request.setTag(tag);
        request.setRetryPolicy(new DefaultRetryPolicy(100000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        BaseApplication.getRequestQueuemanager().add(request);
    }

    // --------------------??????????---------------------------------------


    /**
     * ???????
     *
     * @param url
     * @param map
     * @return lvyb-a  ???
     */
    private String returnGetUrl(String url, Map<String, Object> map) {

        // ????Get?????
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        sb.append("&jsonstring=");
        //sb.append(StringTools.correctEncode(returnGetPara(map)));
        sb.append(returnGetPara(map));
        return sb.toString();
    }

    /**
     * ??????????
     *
     *
     * @param params
     * @return
     */
    public String returnGetPara(Map<String, Object> params) {
        String utfStr = "";
        try {
            utfStr = URLEncoder.encode(gson.toJson(params), Constants.CodeType);//?????????
        } catch (Exception e) {
            utfStr = "";
        }
        return utfStr;
    }

    public void setResponseListener(ResponseInterface mResponseInterface) {
        this.mResponseInterface = mResponseInterface;
    }

    /**
     * ??????
     *
     * @author zhang_q
     */
    public interface ResponseInterface {

        public <T> void successResponse(T parsedGSON, int tag);

        public void errorResonse(String retmeg, int tag);
    }

}
